<?php
namespace Admin\Controller;
use Common\Controller\CommonController;
use Think\Auth;
class AdminController extends CommonController {
	protected $user;
	protected $config;
	public function _initialize(){
		$token = I('token');
		if(empty($token)){
			$back = ['status'=>-1,'msg'=>'请登录'];
			$this->ajaxReturn($back,'JSON');
		}
		$uid = M('Admin_token')->where(['token'=>$token])->find()['uid'];
		if(empty($uid)){
			$back = ['status'=>-2,'msg'=>'未查询到当前用户'];
			$this->ajaxReturn($back,'JSON');
		}
		$user = M('Admin')->where(['admin_id'=>$uid])->find();
		$this->user = $user;
		$config = M('Config')->select();
		foreach ($config as $k=>$v){
			$config_re[$v['key']] = $v['value'];
		}
		$this->config=$config_re;
// 		$AUTH = new Auth();
// 		$name = CONTROLLER_NAME . '/' . ACTION_NAME;
// 		$type = MODULE_NAME;
// 		//执行check的模式
// 		$mode = 'url';
// 		//'or' 表示满足任一条规则即通过验证;
// 		//'and'则表示需满足所有规则才能通过验证
// 		$relation = 'and';
// 		if (!$AUTH->check($name, $uid, $type, $mode, $relation)) {
// 			$back = ['status'=>-1,'msg'=>'无权限'];
// 			//$this->ajaxReturn($back,'JSON');
// 		}
	}
	public function getMemberGroup($member_id){
		return $member_id%10;
	}
	/**
	 * 格式化时间
	 */
// 	public function 
	//图片处理
	public function upload($file){
	    
	    switch($file['type'])
	    {
	        case 'image/jpeg': $ext = 'jpg'; break;
	        case 'image/gif': $ext = 'gif'; break;
	        case 'image/png': $ext = 'png'; break;
	        case 'image/tiff': $ext = 'tif'; break;
	        default: $ext = ''; break;
	    }
	    if (empty($ext)){
	        return false;
	    }
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      './Public/Uploads/'; // 设置附件上传目录
		// 上传文件
		$info   =  $upload->uploadOne($file);
		$data = [];
		if(!$info) {
			// 上传错误提示错误信息
			$data = [
				'status'=>-1,
				'msg'=>$upload->getError(),
			];
		}else{
			// 上传成功
			$pic=$info['savepath'].$info['savename'];
			$url='/Uploads'.ltrim($pic,".");
			$data = [
				'status'=>1,
				'url'=>$url,
			];
		}
		return $data;
	}
	//空操作
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	/**
	 * 处理分页
	 */
	public function DoPage($page,$num){
		if(empty($page)){
			$page = 1;
		}
		if(empty($num)){
			$num = 15;
		}
		$data = [
			'page'=>$page,
			'num'=>$num,
			'limit'=>($page-1)*$num,
		];
		return $data;
	}
	/**
	 * 处理时间
	 */
	public function dotime($begin,$last){
		if(empty($begin)){
			$begin = 0;
		}else{
			$begin = $begin;
		}
		if(empty($last)){
			$last = time();
		}else{
			$last = $last+3600*24;
		}
		$data = [
			'begin'=>$begin,
			'last'=>$last,
		];
		return $data;
	}
	/**
	 * 添加日志
	 */
	public function addAdminLog($uid,$type,$num,$cointype,$admin_id){
		$data['uid']=$uid;
		$data['num']=$num;
		$data['type']=$type;
		$data['cointype']=$cointype;
		$data['add_time']=time();
		$data['admin_id']=$admin_id;
		$r=M('Admin_coin_log')->add($data);
		if($r){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 添加充值日志
	 */
	//dump($sql);
	public function addRechargeLog($uid,$num,$cointype){
	    $data['uid']=$uid;
	    $data['num']=$num;
	    $data['coin_type']=$cointype;
	    $data['add_time']=time();
	    $data['status']=1;
	    $r=M('Coin_pay')->add($data);
	    if($r){
	        return true;
	    }else{
	        return false;
	    }
	}

}



